<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\Admin;//员工模型
use app\admin\model\Feedback;//问题反馈

class Login extends Controller
{
    public function login(Request $request)
    {
        if ($request->isGet()) {
            if (!is_file('static/Admin/dict/dict.json')) {
                $data = getables('dict');
                $this->set_php_file("static/Admin/dict/dict.json", json_encode($data));
            }
            if (!cookie('think_var')) {
                cookie('think_var', 'zh');
            }
            $admin_id = session('userid');//获取管理员id
            $langs = getlangnames();
            if (!$admin_id) {
                $admin_cache = cookie('admin_cache');
                return view('login', ['admin_cache' => $admin_cache, 'langs' => $langs]);
            } else {
                $this->redirect('/');
            }
        } elseif ($request->isAjax() || $request->isPOST()) {
            $data = request()->param();
            $lginfo = new \app\admin\model\Admin();
            //账号 密码
            $info = $lginfo->getLoginStatu($data);
            return json_encode($info);
        }
    }

    //员工的修改密码
    public function editpass(Request $request)
    {
        $data = $request->param();
        $admin = new Admin();
        if ($request->isGet()) {
            return view('login/editpass');
        } elseif ($request->isAjax() && $request->isPost()) {
            $adminret = $admin->editpass($data);
            $udata["status"] = $adminret['status'];
            $udata['msg'] = $adminret['msg'];
            echo json_encode($udata);
        }
    }

    //员工反馈问题
    public function feedback(Request $request)
    {
        $data = $request->param();
        $admindata = session('admin');
        $position_id = $admindata['position_id'];
        $admin = new Admin();
        if ($request->isGet()) {
            return view('login/loginfeedback', [
                'controller' => $data['controller'],
                'action' => $data['action'],
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getmenu'])) {
            //管理员 CEO
            $mapmenu['a.is_menu'] = 1;
            $mapmenu['a.disable'] = 0;
            if ($position_id == 1) {
                //是否展示 是否停用 菜单权限
                $menu = getTree(getlangres('menu', is_lang(), 'a.id,a.pid,b.content', $mapmenu));
            } else {
                //非管理员，ceo
                $manage_auth = $admindata['auth_ids'];
                $mapmenu['a.id'] = array('in', $manage_auth);
                $menu = getTree(getlangres('menu', is_lang(), 'a.id,a.pid,b.content', $mapmenu));
            }
            $menuwhere['control'] = $data['controller'];
            $menuwhere['option'] = $data['action'];
            $menuwhere['disable'] = 0;
            $menu_id = getValue('menu', $menuwhere, 'id');
            $arrmenu=array_column($menu,'id');
            if(!in_array($menu_id,$arrmenu)){
                $menu_id = '';
            }
            return array('menu' => $menu, 'menu_id' => $menu_id);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['savefeedback'])) {
            $adminret = $admin->feedback($data);
            $udata["status"] = $adminret['status'];
            $udata['msg'] = $adminret['msg'];
            echo json_encode($udata);
        }
    }

    //反馈问题的图片上传
    public function messageupload(Request $request)
    {
        $file = $request->file('file');
        $res = uploads($file, 'message');
        echo json_encode($res);
    }
    //个人资料查看
    public function personalmeans(Request $request)
    {
        $data = $request->param();
        $admin = new Admin();
        if ($request->isGet()) {
            $infodata = $admin->getadminper();
            return view('login/means', [
                'info' => $infodata,
            ]);
        } elseif ($request->isAjax() && $request->isPost()) {
            $adminret = $admin->means($data);
            $udata["status"] = $adminret['status'];
            $udata['msg'] = $adminret['msg'];
            echo json_encode($udata);
        }
    }

    //用户退出
    public function logout()
    {
        session(null);
        $this->redirect('admin/login/login');
    }

    //多文件的写入缓存文件中
    private function set_php_file($filename, $content)
    {
        $fp = fopen($filename, "w");
        fwrite($fp, $content);
        fclose($fp);
    }

    //错误提示页面
    public function _empty()
    {
        return view('index/mistake');
    }
}